<HTML><HEAD><TITLE>read_string(+Stream, +Delimiters, ?Length, -String)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Character I/O</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>read_string(+Stream, +Delimiters, ?Length, -String)</H1>
Reads a string from the stream Stream up to a delimiter or up to a
specified length.


<DL>
<DT><EM>Stream</EM></DT>
<DD>Integer (stream number) or Atom (reserved or user-defined                symbolic stream name).
</DD>
<DT><EM>Delimiters</EM></DT>
<DD>String or atom.
</DD>
<DT><EM>Length</EM></DT>
<DD>Integer or variable.
</DD>
<DT><EM>String</EM></DT>
<DD>String or variable.
</DD>
</DL>
<H2>Description</H2>
   A string of characters is read from the input stream Stream up to one
   character which occurs in the delimiter string Delimiters.  This
   character is also consumed, but does not appear in the string which is
   unified with String.

<P>
   Two symbolic Delimiters can be specified:

<P>
<PRE>
    end_of_line   a newline or carriage-return/newline sequence
    end_of_file   the end of the file/input
</PRE>
   End of file always acts like a delimiter.

<P>
   If Length is a variable, it is unified with the length of the string
   String.  If Length is an integer, the number of characters read from
   the input stream Stream is limited by Length.

<P>

<H3>Modes and Determinism</H3><UL>
<LI>read_string(+, +, +, -) is semidet
<LI>read_string(+, +, -, -) is semidet
</UL>
<H3>Fail Conditions</H3>
There is nothing to read, i.e. the stream is at end_of_file
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Delimiters is not instantiated.
<DT><EM>(5) type error </EM>
<DD>Delimiters is not a string or atom.
<DT><EM>(5) type error </EM>
<DD>Length is a non-integer number.
<DT><EM>(5) type error </EM>
<DD>String is not a variable or a string.
<DT><EM>(6) out of range </EM>
<DD>Delimiters is an atom but not a valid symbolic delimiter.
<DT><EM>(24) number expected </EM>
<DD>Length is not a variable or number.
<DT><EM>(190) end of file reached </EM>
<DD>End of file was encountered before reading any character.
<DT><EM>(192) illegal stream mode </EM>
<DD>Stream is not an input stream.
<DT><EM>(193) illegal stream specification </EM>
<DD>Stream is an illegal stream specification.
<DT><EM>(198) reading past the file end </EM>
<DD>Trying to read even after the error 190 was raised.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      [eclipse]: read_string(input, "123", Length, String).
          abcdef2ghi

      Length = 6
      String = "abcdef"
      yes.
      [eclipse]: read_string(input, " \t", Length, String).
      one two

      Length = 3
      String = "one"
      yes.
      [eclipse]: read_string(input, end_of_line, Length, String).
      abcdefghi

      Length = 9
      String = "abcdefghi"
      yes.
      [eclipse]: read_string(input, end_of_line, 6, String).
      abcdefghi

      String = "abcdef"
      yes.

      [eclipse]: open(file1, read, s).

      yes.
      [eclipse]: system('cat file1').
      abcd

      yes.
      [eclipse]: read_string(s, "", Length, String).

      Length = 5
      String = "abcd\n"           % Read up to end of file
      yes.


% Code example: read lines from a file and return
% a list of strings, each string containing a line

    get_lines(File, Lines) :-
	open(File, read, S),
	get_lines(S, Lines),
	close(S).

    get_lines(S, Lines) :-
	( read_string(S, end_of_line, _, Line) -&gt;
	    Lines = [Line|Ls],
	    get_lines(S, Ls)
	;
	    Lines = []
	).


Fail:
      [eclipse]: open(string(""),read,s), read_string(s,"",L,String).

      no (more) solution.         % EOF - Error 190 - handler fails

Error:
    read_string(Stream, "", Length, String).       (Error 4).
    read_string(stream, Dels, Length, String).     (Error 4).
    read_string("stream", "", Length, String).     (Error 5).
    read_string(stream, 12, Length, String).       (Error 5).
    read_string(stream, "", "abc", String).        (Error 5).
    read_string(stream, "", Length, 12).           (Error 5).
    read_string(stream, stop, Length, String).     (Error 6).
    read_string(output, "", Length, String).       (Error 192).
    read_string(atom, "", Length, String).         (Error 193).



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/iochar/read_string-3.html">read_string / 3</A>, <A HREF="../../kernel/iochar/read_token-2.html">read_token / 2</A>, <A HREF="../../kernel/iochar/read_token-3.html">read_token / 3</A>, <A HREF="../../kernel/iostream/open-3.html">open / 3</A>
</BODY></HTML>
